package com.yuanqi.dz.dc.service.impl;

/**
 * 新版获取支付宝对账数据接口
 */
import java.util.List;
import pnbclient.helper.StringHelper;
import pnbclient.util.PnbLogFactory;
import pnblog.PnbLogging;
import com.powermop.service.info.GetAlipayDzInInfo;
import com.powermop.service.info.GetAlipayDzOutInfo;
import com.powermop.service.remote.IRefundInforMation;
import com.yuanqi.dz.dc.util.DZConfigRead;
import com.yuanqitech.powermop.dz.DZDataInterface;
import com.yuanqitech.powermop.dz.DZInfo;
import com.yuanqitech.powermop.dz.DZInfoArray;

public class ZfbDZServiceImpl implements DZDataInterface {
	public static final PnbLogging LOG = PnbLogFactory.getLogger("DZ_EXT_LOG");

	public static void main(String[] args) {
		DZInfoArray d= new ZfbDZServiceImpl().getDZData("2088121689754290", "20170601");
		System.out.println(d.getErrormsg());
		DZInfo[] dd=  d.getDzInfo();
		if(null!=dd && dd.length >0){
			for (int i = 0; i < dd.length; i++) {
				System.out.println(dd[i]);
			}	
		}
		

	}

	public DZInfoArray getDZData(String mercharnum, String dzdate) {
		DZInfoArray dzInfoArray = new DZInfoArray();
		GetAlipayDzInInfo  info = new GetAlipayDzInInfo();
		info.setMechart_num(mercharnum);
		info.setDzdate(dzdate);//zftx_dz_address  zfb_dz_address
		String zfb_dz_address = DZConfigRead.getProperties("zftx_dz_address");
		if(StringHelper.isEmpty(zfb_dz_address)){
			dzInfoArray.setRc("-1");
			dzInfoArray.setErrormsg("获取支付宝对账数据HS地址未配置");
			return dzInfoArray;
		}
		LOG.addDebug("create " + IRefundInforMation.class.getSimpleName() + " from:" + zfb_dz_address);
		GetAlipayDzOutInfo out = null;
		try {
			LOG.addInfo("调用HS接口("+zfb_dz_address+")入参{mercharnum:"+mercharnum+",dzdate:"+dzdate+"}");
			IRefundInforMation iRefundInforMation = (IRefundInforMation) pnbclient.util.HSClient.create(IRefundInforMation.class, zfb_dz_address);
			out = iRefundInforMation.GetAlipayDz(info);
			if(null == out){
				LOG.addError("获取支付宝数据为null");
				dzInfoArray = new DZInfoArray();
				dzInfoArray.setRc("-1");
				dzInfoArray.setErrormsg("HS获取支付宝对账数据为null");
				return dzInfoArray;
			}
			String rc= out.getRc();
			LOG.addInfo("返回rc:"+rc);
			if(null!=rc && "1".equals(rc)){
				List<DZInfo> list = out.getDzlist();
				if(null == list || list.size() ==0){
					LOG.addInfo("返回rc:1，list为null");
					dzInfoArray = new DZInfoArray();
					dzInfoArray.setRc("-1");
					dzInfoArray.setErrormsg("HS获取支付宝对账数据为空");
					return dzInfoArray;
				}
				dzInfoArray = new DZInfoArray();
				dzInfoArray.setRc("1");
				dzInfoArray.setErrormsg("获取支付宝对账数据成功!");
				dzInfoArray.setDzInfo(list.toArray(new DZInfo[list.size()]));

			}else{
				dzInfoArray = new DZInfoArray();
				dzInfoArray.setRc("-1");
				dzInfoArray.setErrormsg(StringHelper.isEmpty(out.getErrormsg())?"获取支付宝对账数据失败":out.getErrormsg());
				dzInfoArray.setDzInfo(new DZInfo[0]);
			}
			LOG.addInfo("获取支付宝对账数据结果:{msg:"+dzInfoArray.getErrormsg()+",size:"+dzInfoArray.getDzInfo().length+"}");
		} catch (Exception e) {
			LOG.addError(e);
			dzInfoArray = new DZInfoArray();
			dzInfoArray.setRc("-1");
			dzInfoArray.setErrormsg("HS获取支付宝对账数据异常!");
			return dzInfoArray;
		}

		return dzInfoArray;
	}

}
